<?php
namespace app\api\controller;
use think\facade\Db;
use think\captcha\facade\Captcha;
class Admin extends Base{
    // 登录后台
    public function loginAdmin(){
        $pt = input("post.");
        $captcha = new Captcha();
        if($captcha::check($pt['code'])){

            $res = $this->getResult(1,3);
            if (empty($res)) {
                $this->info['code']=201;
                $this->info['msg']='请求太快，请3秒之后再试';
                return json($this->info);
            }

            $where = [
                'user'=>trim($pt['user']),
                'pass'=>md5('anan'.md5(trim($pt['pass'])))
            ];
            $adminData = Db::table('an_admin')
            ->where($where)
            ->field('id,name,user,login_time,role_id,a_show')
            ->find(); 
            if($adminData){
                if($adminData['a_show'] == '1'){
                    $this->logData('登录系统',$adminData['id']);
                    Db::table('an_admin')
                    ->where('id', $adminData['id'])
                    ->data(['login_time'=>time()])
                    ->update();
                    $this->info['code'] = 200;
                    $this->info['msg'] = '登录成功';
                    $this->info['data']['admin'] = $adminData;
                    $this->info['data']['token'] = signToken($adminData['id']);
                    return json($this->info);
                }else{
                    $this->info['code'] = 202;
                    $this->info['msg'] = '账号已禁用，请联系管理员';
                    return json($this->info);
                }
            }else{
                $this->info['code'] = 201;
                $this->info['msg'] = '账号或密码有误';
                return json($this->info);
            }
        }else{
            $this->info['code'] = 201;
            $this->info['msg'] = '验证码有误';
            return json($this->info);
        }
    }

    //重置密码
    public function resetPass(){
        $param = input('');
        $this->info['data'] = Db::table('an_admin')
        ->where('id', $param['id'])
        ->update(['pass'=>md5('anan'.md5('123456'))]);
        $this->info['msg'] = '重置成功';
        return json($this->info);
    }
    

    //修改登录密码
    public function upPass_a(){
        $param = input('');
        $pass = md5('anan'.md5(trim($param['pass_a'])));
        $where = [
            'id'=>$this->userInfo()['uid'],
            'pass'=> md5('anan'.md5(trim($param['oldpass'])))
        ];
        $adminData = Db::table('an_admin')->where($where)->select()->toArray();
        if(count($adminData) > 0){
            $bool = Db::table('an_admin')->where('id', $this->userInfo()['uid'])->data(['pass'=>$pass])->update();
            session('adminData', null);
            $this->logData('修改系统登录密码');
            $this->info['code']=200;
            $this->info['msg']='修改成功';
        }else{
            $this->info['code']=201;
            $this->info['data']=$adminData;
            $this->info['msg']='修改失败,原密码输入有误';
        }
        return json($this->info);
    }


    public function list_data_a(){
        $param = input("");
        $this->info['data'] = [
            'list' => Db::table('an_admin')->order('id', 'desc')->select()->toArray(),
            'type' => Db::table('an_type')->where('type', 'role')->order('id', 'desc')->select()->toArray()
        ];
        $this->info['total'] = Db::table('an_admin') ->count();
        return json($this->info);
    }

    //添加 / 更新数据
    public function saveData(){
        $param = input('');
        $data = [
            'up_time'=>time(),
            'role_id'=>$param['role_id'],
            "name"=>$param['name'],
            "user"=>$param['user'],
            "notes"=>$param['notes'],
            "a_show"=>$param['a_show'],
        ];
        if(empty($param['id'])){
            $data['create_time'] = time();
            $data['pass'] = md5('anan'.md5('123456'));
            $this->info['msg']='新增管理员成功';
        }else{
            $data['id'] = $param['id'];
            $this->info['msg']='修改管理员成功';
        }
        $this->logData($this->info['msg'].'，管理员：'.$param['name']);

        //save 自动判断是编辑更新数据 还是 插入新数据
        $this->info['data'] = Db::table('an_admin')->save($data);
        return json($this->info);
    } 

    //删除数据
    public function del_a(){
        $param = input('');
        $this->info['data'] = Db::table('an_admin')->where('id',$param['id'])->delete();
        return json($this->info);
    }

    //获取所有权限列表
    public function getNode(){
        $param = input("");
        $this->info['data'] = [
            'list' => Db::table('an_admin_node')->order('sort', 'desc')->select()->toArray(),
            'type' => []
        ];
        $this->info['total'] = Db::table('an_admin_node') ->count();
        return json($this->info);
    }


    //获取角色权限列表
    public function getRoleNode(){
        $param = input("");

        //根据token里面的id获取登录的用户角色
        $admin = Db::table('an_admin')
        ->where('id',$this->userInfo()['uid'])
        ->find();

        //判断用户角色
        if($admin['role_id'] == 999999999){
            $this->info['data'] = Db::table('an_admin_node')
            ->order('sort', 'desc')
            ->select()
            ->toArray();
        }else{
            //根据角色获取权限ID
            $roleID = Db::table('an_type')
            ->where('id',$admin['role_id'])
            ->where('a_show', '1')
            ->find();

            //根据权限ID 获取所有权限信息
            $this->info['data'] = Db::table('an_admin_node')
            ->order('sort', 'desc')
            ->where('id','in',$roleID['other'])
            ->select()
            ->toArray();
        }
        return json($this->info);
    }

    //添加 、 编辑权限
    public function saveNode(){
        $param = input('');
        $data = [
            'up_time'=>time(),
            'pid'=>$param['pid'],
            "name"=>$param['name'],
            "url"=>$param['url'],
            "img"=>$param['img'],
            "other"=>$param['other'],
            "type"=>$param['type'],
            "sort"=>$param['sort'],
            "a_show"=>$param['a_show'],
            "component"=>$param['component'],
            "lang"=>$param['lang'],
        ];
        if(empty($param['id'])){
            $data['create_time'] = time();
            $this->info['msg']='新增权限成功';
        }else{
            $data['id'] = $param['id'];
            $this->info['msg']='修改权限成功';
        }
        $this->logData($this->info['msg'].'，权限：'.$param['name']);

        //save 自动判断是编辑更新数据 还是 插入新数据
        $this->info['data'] = Db::table('an_admin_node')->save($data);
        return json($this->info);
    }

    //删除数据
    public function delNode(){
        $param = input('');
        foreach($param['ids'] as $v){
            Db::table('an_admin_node')->where('id',$v)->delete();
        }
        return json($this->info);
    }
}